# -*- coding;utf-8 -*-
"""
File name : tasks.py.PY
Program IDE : PyCharm
Create file time: 2023/10/19 23:53
File Create By Author : 朱清闯
"""
from celery_tasks.main import app
from question.models import DownloadLog, DownloadExaminationPaperLog
from question.serializers import DownloadGuidePaperLogModelSerializer, DownloadQuestionLogModelSerializer
from question.serializers import DownloadExaminationPaperLogModelSerializer


@app.task(name='create_download_log')
def create_download_log(resource, user):
    DownloadLog.objects.create(resource=resource, user=user)


# 创建下载题目记录
@app.task(name='create_download_question_log')
def create_download_question_log(question, user):
    data = {
        'question': question,
        'user': user,
       }
    serializer = DownloadQuestionLogModelSerializer(data=data)
    if serializer.is_valid():
        serializer.save()
    else:
        print('异步任务创建库币交易记录失败error')


# 创建下载导学案记录
@app.task(name='create_download_guide_paper_log')
def create_download_guide_paper_log(guide_paper, user):
    data = {
        'guide_paper': guide_paper,
        'user': user,
    }
    serializer = DownloadGuidePaperLogModelSerializer(data=data)
    if serializer.is_valid():
        serializer.save()
    else:
        print('异步任务创建下载导学案记录失败error', serializer.error_messages)


# 创建下载试卷记录
@app.task(name='create_download_examination_paper_log')
def create_download_examination_paper_log(examination_paper, user):
    # DownloadExaminationPaperLog.objects.create(examination_paper=examination_paper, user=user)
    new_data = {
        'examination_paper': examination_paper,
        'user': user,
    }
    serializer = DownloadExaminationPaperLogModelSerializer(data=new_data)
    if serializer.is_valid():
        serializer.save()
    else:
        print('异步任务创建下载试卷记录失败error', serializer.error_messages)
